<%
'******************************************************************************
' Copyright (C) 2006 Google Inc.
'  
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'  
'      http://www.apache.org/licenses/LICENSE-2.0
'  
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.
'******************************************************************************

'******************************************************************************
' Class Order is the object representation of an Order Processing API command:
'     Financial Commands:
'         <charge-order>
'         <refund-order>
'         <cancel-order>
'         <authorize-order>
'     Fulfillment Commands:
'         <process-order>
'         <add-merchant-order-number>
'         <deliver-order>
'         <add-tracking-data>
'         <send-buyer-message>
'     Line-item shipping commands:
'         <backorder-items>
'         <cancel-items>
'         <return-items>
'         <reset-item-shipping-information>
'         <ship-items>
'     Archiving Commands:
'         <archive-order>
'         <unarchive-order>
'******************************************************************************
Class Order

	Private OrderCommand

	'**************************************************************************
	' Creates a <charge-order> command
	' 
	' Input:    GoogleOrderNumber    Required: <google-order-number>
	'           Amount               Optional: <amount>
	'**************************************************************************
	Public Sub ChargeOrder(GoogleOrderNumber, Amount)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		If Amount <> "" Then
			Xml.Push "charge-order", Attr
			Attr = Xml.Attribute("currency", MerchantCurrency)
			Xml.AddElement "amount", Amount, Attr
			Xml.Pop "charge-order"
		Else
			Xml.EmptyElement "charge-order", Attr
		End If
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <refund-order> command
	' 
	' Input:    GoogleOrderNumber    Required: <google-order-number>
	'           Reason               Required: <reason>
	'           Comment              Optional: <comment>
	'           Amount               Optional: <amount>
	'**************************************************************************
	Public Sub RefundOrder(GoogleOrderNumber, Reason, Comment, Amount)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.Push "refund-order", Attr
		Xml.AddElement "reason", Reason, ""
		If Comment <> "" Then
			Xml.AddElement "comment", Comment, ""
		End If
		If Amount <> "" Then
			Attr = Xml.Attribute("currency", MerchantCurrency)
			Xml.AddElement "amount", Amount, Attr
		End If
		Xml.Pop "refund-order"
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <cancel-order> command
	' 
	' Input:    GoogleOrderNumber    Required: <google-order-number>
	'           Reason               Required: <reason>
	'           Comment              Optional: <comment>
	'**************************************************************************
	Public Sub CancelOrder(GoogleOrderNumber, Reason, Comment)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.Push "cancel-order", Attr
		Xml.AddElement "reason", Reason, ""
		If Comment <> "" Then
			Xml.AddElement "comment", Comment, ""
		End If
		Xml.Pop "cancel-order"
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <authorize-order> command
	' 
	' Input:    GoogleOrderNumber    Required: <google-order-number>
	'**************************************************************************
	Public Sub AuthorizeOrder(GoogleOrderNumber)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.EmptyElement "authorize-order", Attr
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <process-order> command
	' 
	' Input:    GoogleOrderNumber    Required: <google-order-number>
	'**************************************************************************
	Public Sub ProcessOrder(GoogleOrderNumber)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.EmptyElement "process-order", Attr
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <add-merchant-order-number> command
	' 
	' Input:    GoogleOrderNumber      Required: <google-order-number>
	'           MerchantOrderNumber    Required: <merchant-order-number>
	'**************************************************************************
	Public Sub AddMerchantOrderNumber(GoogleOrderNumber, MerchantOrderNumber)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.Push "add-merchant-order-number", Attr
		Xml.AddElement "merchant-order-number", MerchantOrderNumber, ""
		Xml.Pop "add-merchant-order-number"
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <deliver-order> command
	' 
	' Input:    GoogleOrderNumber      Required: <google-order-number>
	'           Carrier                Optional: <merchant-order-number>
	'           TrackingNumber         Optional: <tracking-number>
	'           SendEmail              Optional: <send-email>; Boolean
	'**************************************************************************
	Public Sub DeliverOrder(GoogleOrderNumber, Carrier, TrackingNumber, SendEmail)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)

		If Carrier <> "" Or SendEmail Then
			Xml.Push "deliver-order", Attr
			If Carrier <> "" Then
				Xml.Push "tracking-data", ""
				Xml.AddElement "carrier", Carrier, ""
				Xml.AddElement "tracking-number", TrackingNumber, ""
				Xml.Pop "tracking-data"
			End If 
			If SendEmail Then
				Xml.AddElement "send-email", "true", ""
			End If
			Xml.Pop "deliver-order"
		Else
			Xml.EmptyElement "deliver-order", Attr
		End If
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <add-tracking-data> command
	' 
	' Input:    GoogleOrderNumber      Required: <google-order-number>
	'           Carrier                Required: <merchant-order-number>
	'           TrackingNumber         Required: <tracking-number>
	'**************************************************************************
	Public Sub AddTrackingData(GoogleOrderNumber, Carrier, TrackingNumber)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.Push "add-tracking-data", Attr
		Xml.Push "tracking-data", ""
		Xml.AddElement "carrier", Carrier, ""
		Xml.AddElement "tracking-number", TrackingNumber, ""
		Xml.Pop "tracking-data"
		Xml.Pop "add-tracking-data"
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub
	
	'**************************************************************************
	' Creates a <send-buyer-message> command
	' 
	' Input:    GoogleOrderNumber      Required: <google-order-number>
	'           Message                Required: <message>
	'           SendEmail              Optional: <send-email>; Boolean
	'**************************************************************************
	Public Sub SendBuyerMessage(GoogleOrderNumber, Message, SendEmail)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.Push "send-buyer-message", Attr
		Xml.AddElement "message", Message, ""
		If SendEmail Then
			Xml.AddElement "send-email", "true", ""
		End If
		Xml.Pop "send-buyer-message"
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <backorder-items> command
	' 
	' Input:    GoogleOrderNumber      Required: <google-order-number>
	'           ItemIds                Required: Array of merchant item ids
	'           SendEmail              Optional: <send-email>; Boolean
	'**************************************************************************
	Public Sub BackOrderItems(GoogleOrderNumber, ItemIds, SendEmail)
		LineItemCommand "backorder-items", GoogleOrderNumber, ItemIds, "", "", SendEmail
	End Sub

	'**************************************************************************
	' Creates a <cancel-items> command
	' 
	' Input:    GoogleOrderNumber      Required: <google-order-number>
	'           ItemIds                Required: Array of merchant item ids
	'           SendEmail              Optional: <send-email>; Boolean
	'**************************************************************************
	Public Sub CancelItems(GoogleOrderNumber, ItemIds, Reason, Comment, SendEmail)
		LineItemCommand "cancel-items", GoogleOrderNumber, ItemIds, Reason, Comment, SendEmail
	End Sub

	'**************************************************************************
	' Creates a <return-items> command
	' 
	' Input:    GoogleOrderNumber      Required: <google-order-number>
	'           ItemIds                Required: Array of merchant item ids
	'           SendEmail              Optional: <send-email>; Boolean
	'**************************************************************************
	Public Sub ReturnItems(GoogleOrderNumber, ItemIds, SendEmail)
		LineItemCommand "return-items", GoogleOrderNumber, ItemIds, "", "", SendEmail
	End Sub

	'**************************************************************************
	' Creates a <reset-items-shipping-information> command
	' 
	' Input:    GoogleOrderNumber      Required: <google-order-number>
	'           ItemIds                Required: Array of merchant item ids
	'           SendEmail              Optional: <send-email>; Boolean
	'**************************************************************************
	Public Sub ResetItemsShippingInfo(GoogleOrderNumber, ItemIds, SendEmail)
		LineItemCommand "reset-items-shipping-information", GoogleOrderNumber, ItemIds, "", "", SendEmail
	End Sub

	'**************************************************************************
	' Creates a line-item order command
	' 
	' Input:    Command                Required: command type
	'           GoogleOrderNumber      Required: <google-order-number>
	'           ItemIds                Required: Array of merchant item IDs
	'           SendEmail              Optional: <send-email>; Boolean
	'**************************************************************************
	Private Sub LineItemCommand(Command, GoogleOrderNumber, ItemIds, Reason, Comment, SendEmail)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.Push Command, Attr
		Xml.Push "item-ids", ""
		Dim MerchantItemId
		For Each MerchantItemId In ItemIds
		  Xml.Push "item-id", ""
		  Xml.AddElement "merchant-item-id", MerchantItemId, ""
		  Xml.Pop "item-id"
		Next
		Xml.Pop "item-ids"
		If Reason <> "" Then
		  Xml.AddElement "reason", Reason, ""
		End If
		If Comment <> "" Then
		  Xml.AddElement "comment", Comment, ""
		End If
		If SendEmail Then
				Xml.AddElement "send-email", "true", ""
		End If
		Xml.Pop Command
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <ship-items> command
	' 
	' Input:    GoogleOrderNumber       Required: <google-order-number>
	'           ItemShippingInfoList    Optional: ItemShippingInfoList
	'           SendEmail               Optional: <send-email>; Boolean
	'**************************************************************************
	Public Sub ShipItems(GoogleOrderNumber, ItemShippingInfoList, SendEmail)
		Dim Xml, Attr, i
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.Push "ship-items", Attr
		Xml.Push "item-shipping-information-list", ""
		If ItemShippingInfoList.ItemShippingInfoIndex >= 0 Then
			Dim ItemShippingInfo
			For Each ItemShippingInfo In ItemShippingInfoList.ItemShippingInfoArr
			  Xml.Push "item-shipping-information", ""
			  Xml.Push "item-id", ""
			  Xml.AddElement "merchant-item-id", ItemShippingInfo.ItemId, ""
			  Xml.Pop "item-id"
			  If ItemShippingInfo.TrackingDataIndex >= 0 Then
				Xml.Push "tracking-data-list", ""
				For i = 0 To ItemShippingInfo.TrackingDataIndex
				  Xml.Push "tracking-data", ""
				  Xml.AddElement "carrier", ItemShippingInfo.Carriers(i), ""
				  Xml.AddElement "tracking-number", ItemShippingInfo.TrackingNumbers(i), ""
				  Xml.Pop "tracking-data"
				Next
				Xml.Pop "tracking-data-list"
			  End If
			  Xml.Pop "item-shipping-information"
			Next
			Set ItemShippingInfo = Nothing
		End If
		Xml.Pop "item-shipping-information-list"
		If SendEmail Then
			Xml.AddElement "send-email", "true", ""
		End If
		Xml.Pop "ship-items"
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <archive-order> command
	' 
	' Input:    GoogleOrderNumber    Required: <google-order-number>
	'**************************************************************************
	Public Sub ArchiveOrder(GoogleOrderNumber)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.EmptyElement "archive-order", Attr
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Creates a <unarchive-order> command
	' 
	' Input:    GoogleOrderNumber    Required: <google-order-number>
	'**************************************************************************
	Public Sub UnarchiveOrder(GoogleOrderNumber)
		Dim Xml, Attr
		Set Xml = New XmlBuilder
		Attr = Xml.Attribute("xmlns", SchemaUri)
		Attr = Attr & " " & Xml.Attribute("google-order-number", GoogleOrderNumber)
		Xml.EmptyElement "unarchive-order", Attr
		OrderCommand = Xml.GetXml
		Set Xml = Nothing
	End Sub

	'**************************************************************************
	' Sends the order processing API command to Google Checkout.
	'**************************************************************************
	Public Sub SendOrderCommand()
		Dim ResponseXml
		ResponseXml = SendRequest(OrderCommand, PostUrl)
		ProcessSynchronousResponse(ResponseXml)
	End Sub

	'**************************************************************************
	' Diagnoses the order processing command XML by posting to 
	'     the Diagnose URL.
	'**************************************************************************
	Public Sub DiagnoseXml()
		Dim ResponseXml
		ResponseXml = SendRequest(OrderCommand, DiagnoseUrl)
		ProcessSynchronousResponse(ResponseXml)
	End Sub

	'**************************************************************************
	' Processes the synchronous response received from Google Checkout 
	'     when an order processing command is issued.
	'**************************************************************************
	Public Sub ProcessSynchronousResponse(ResponseXml)
		Dim ResponseNode, RootTag
		Set ResponseNode = GetRootNode(ResponseXml)
		RootTag = ResponseNode.Tagname

		Select Case RootTag
			Case "request-received"
				DisplayResponse ResponseXml
			Case "error"
				DisplayResponse ResponseXml
			Case "diagnosis"
				DisplayResponse ResponseXml
			Case Else
		End Select 
	End Sub

	'**************************************************************************
	' Displays the response in the browser.
	'**************************************************************************
	Public Sub DisplayResponse(ResponseXml)
		Response.Write "<pre>"
		Response.Write Server.HTMLEncode(ResponseXml)
		Response.Write "</pre>"
	End Sub

	'**************************************************************************
	' Returns the order processing API command XML in plain text format.
	'**************************************************************************
	Public Property Get GetXml()
		GetXml = OrderCommand
	End Property

End Class

'******************************************************************************
' Class ItemShippingInfoList is the object representation of a 
'     <item-shipping-information-list>.
'******************************************************************************
Class ItemShippingInfoList
	Public ItemShippingInfoArr()
	Public ItemShippingInfoIndex

	Private Sub Class_Initialize()
		ItemShippingInfoIndex = -1
	End Sub
  
	'**************************************************************************
	' Adds an ItemShippingInfo to the ItemShippingInfoList object.
	'
	' Input:    ItemShippingInfo    Object: Class ItemShippingInfo
	'**************************************************************************
	Public Sub AddItemShippingInfo(ItemShippingInfo)
		ItemShippingInfoIndex = ItemShippingInfoIndex + 1
		ReDim Preserve ItemShippingInfoArr(ItemShippingInfoIndex)
		Set ItemShippingInfoArr(ItemShippingInfoIndex) = ItemShippingInfo
	End Sub

	Private Sub Class_Terminate()
		Erase ItemShippingInfoArr
	End Sub
End Class

'******************************************************************************
' Class ItemShippingInfo is the object representation of a 
'     <item-shipping-information>.
'******************************************************************************
Class ItemShippingInfo

	Public ItemId
	Public Carriers()
	Public TrackingNumbers()
	Public TrackingDataIndex

	Private Sub Class_Initialize()
		TrackingDataIndex = -1
	End Sub
  
	'**************************************************************************
	' Adds a tracking-data to the ItemShippingInfo object
	'
	' Input:    Carrier         "UPS", "FedEx", "USPS", etc.
	'           TrackingNumber  Tracking number
	'**************************************************************************
	Public Sub AddTrackingData(Carrier, TrackingNumber)
		TrackingDataIndex = TrackingDataIndex + 1
		ReDim Preserve Carriers(TrackingDataIndex)
		ReDim Preserve TrackingNumbers(TrackingDataIndex)
		Carriers(TrackingDataIndex) = Carrier
		TrackingNumbers(TrackingDataIndex) = TrackingNumber
	End Sub

	Private Sub Class_Terminate()
		Erase Carriers
 		Erase TrackingNumbers
	End Sub
End Class


%>